<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图组件</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
            margin: 0;
        }

        .carousel {
            position: relative;
            width: 600px;
            height: 400px;
            overflow: hidden;
            border: 2px solid #ccc;
            border-radius: 10px;
        }

        .slides {
            display: flex;
            transition: transform 0.5s ease;
            width: 100%;
        }

        .slide {
            min-width: 100%;
            height: 400px;
            background-size: cover;
            background-position: center;
        }

        .buttons {
            position: absolute;
            top: 50%;
            width: 100%;
            display: flex;
            justify-content: space-between;
            transform: translateY(-50%);
        }

        .button {
            background-color: rgba(255, 255, 255, 0.8);
            border: none;
            padding: 10px;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s;
        }

        .button:hover {
            background-color: rgba(255, 255, 255, 1);
        }
    </style>
</head>
<body>
    <div class="carousel">
        <div class="slides">
            <div class="slide" style="background-color: red;"></div>
            <div class="slide" style="background-color: blue;"></div>
            <div class="slide" style="background-color: azure;"></div>
            <div class="slide" style="background-color: green;"></div> <!-- 添加额外幻灯片 -->
        </div>
        <div class="buttons">
            <button class="button" id="prev">◀</button>
            <button class="button" id="next">▶</button>
        </div>
    </div>

    <script>
        const slides = document.querySelector('.slides');
        const slideCount = document.querySelectorAll('.slide').length;
        let currentIndex = 0;

        document.getElementById('next').addEventListener('click', () => {
            currentIndex = (currentIndex + 1) % slideCount;
            updateCarousel();
        });

        document.getElementById('prev').addEventListener('click', () => {
            currentIndex = (currentIndex - 1 + slideCount) % slideCount;
            updateCarousel();
        });

        function updateCarousel() {
            const offset = -currentIndex * 100;
            slides.style.transform = `translateX(${offset}%)`;
        }
    </script>
</body>
</html>
